package cn.itcast.hotel;


import cn.itcast.hotel.config.HotelConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpHost;


import org.apache.lucene.index.Terms;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;

import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.TestComponent;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@TestComponent

public class ESIndexDome {
    //全局
    private RestHighLevelClient client;

    @Test
    public void agg() throws IOException{
        // 1.创建Request对象
        SearchRequest request = new SearchRequest("hotel1");
        SearchSourceBuilder source = request.source();
        // 2.2.设置size
        source.size(0);
       source.aggregation(
                AggregationBuilders.terms("brandAgg")
                        .field("brand")
                        .size(20)
        );
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        Aggregations aggregations = response.getAggregations();
        Terms terms = aggregations.get("brandAgg");
    }

    /**
     * 创建索引库
     * @throws IOException
     */
    @Test
    public void createIndex() throws IOException {
        // 1.创建Request对象
        CreateIndexRequest request = new CreateIndexRequest("hotel1");
        Map map = new HashMap<>();
        map.put("number_of_shards",3);
        map.put("number_of_replicas",2);
        // 2.准备请求的参数：DSL语句
        request.source(HotelConstants.MAPPING_TEMPLATE, XContentType.JSON);
        //发送请求
        CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
        System.out.println("创建索引库结果"+response.isAcknowledged());

    }

    //初始化RestHighLevelClient
    @BeforeEach
    public void setup() {
        this.client = new RestHighLevelClient(RestClient.builder(
                HttpHost.create("http://180.76.145.80:9200"),
                HttpHost.create("http://180.76.145.80:9201"),
                HttpHost.create("http://180.76.145.80:9202")
        ));
    }

    /**
     * 关闭流
     * @throws IOException
     */
    @AfterEach
    void tearDown() throws IOException {
        this.client.close();
    }

    /**
     * 查询索引库
     * @throws IOException
     */
    @Test
    public void existIndex() throws IOException {
        //什么样的操作就创建什么样的对象
        GetIndexRequest request = new GetIndexRequest("hotel1");
        // 发送请求
        boolean exists = client.indices().exists(request,RequestOptions.DEFAULT);
        //输出
        System.out.println(exists?"索引库存在":"索引库不存在");
    }

    /**
     * 删除索引库
     * @throws IOException
     */
    @Test
    public void deleteIndex() throws IOException {
        // 1.创建Request对象
        DeleteIndexRequest request = new DeleteIndexRequest("hotel");
        // 2.发送请求
        AcknowledgedResponse response = client.indices().delete(request,RequestOptions.DEFAULT);
        System.out.println(response);
    }
}
